#include "convert.h"
#include <windows.h>
#include <iostream>
#include <string.h>
#include <stdio.h>
#include <io.h>
#include <direct.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <WinBase.h> 


using namespace std;

#define TDX_PATH_FORMAT "G:\\Dev\\test\\data\\tdx_vipdoc\\%s\\lday"
#define CLWW_PATH_FORMAT "G:\\Dev\\stock\\clww2\\Bin\\data\\%s\\day"


#if 0
int convert(char *code)
{
	int fd;
	char clww_path[200], tdx_path[200];
	HRESULT result;
	LPCTSTR pszTdxFormat = TEXT(TDX_PATH_FORMAT);
	LPCTSTR pszClwwFormat = TEXT(CLWW_PATH_FORMAT);

	
	if (code[0] == '6') {
		sprintf_s((LPTSTR)tdx_path, 199, pszTdxFormat, "sh", code);
		StringCchPrintf((LPTSTR)clww_path, 199, pszClwwFormat, "sh", code);
	} else {
		StringCchPrintf((LPTSTR)tdx_path, 199, pszTdxFormat, "sz", code);
		StringCchPrintf((LPTSTR)clww_path, 199, pszClwwFormat, "sz", code);
	}

#if 0
	LPCTSTR pszFormat = TEXT("%s%s%s%s");
	TCHAR* pszText = TEXT("sh\\xday\\sh");
	LPCTSTR pszFormat2 = TEXT(TDX_PATH"sh\\xday\\sh""%s"".day");


	if (code[0] == '6') {
		
		result = StringCchPrintf((LPTSTR)tdx_path, 199, pszFormat, TDX_PATH, "sh\\lday\\sh", code, ".day");
		if (result != S_OK)
			cout << "111" << endl;
		result = StringCchPrintf((LPTSTR)clww_path, 199, pszFormat2, code);	
		if (result != S_OK)
			cout << "222" << endl;		
	} else {
		StringCbPrintf((LPTSTR)tdx_path, 199, pszFormat, TDX_PATH, "sh\\lday\\sh", code, ".day");
		StringCbPrintf((LPTSTR)clww_path, 199, pszFormat, CLWW_PATH, "sh\\day\\sh", code, ".day");	
	}
#endif

	size_t sz;
	StringCbLength((LPTSTR)tdx_path, 199, &sz);

	fd = open(tdx_path, 0x100);


	return 1;
}
#endif

int mkdir_reverse(char *path)
{
	char *p;

	p = path;
	while (p = strchr(p + 1, '\\')) {
		*p = '\0';
		if (access(path, 0) != 0) {
			if (mkdir (path) != 0) {
				*p = '\\';
				return -1;
			}
		}
		
		*p = '\\';
	}

	if (access(path, 0) != 0) {
		if (mkdir (path) != 0) {
			return -1;
		}
	}	

	return 0;
}

#define FILE_NAME_MAX 	(255)
int convert(char *code)
{
	int tdxlen, clwwlen, len;
	char market[6];
	char tdx_path[FILE_NAME_MAX], clww_path[FILE_NAME_MAX];
	int tdxfd = -1, clwwfd = -1;
	struct tdx_day tdxdata;
	struct clww_day clwwdata;

	if (code[0] == '6') {
		strcpy_s(market, 6, "sh");
	} else {
		strcpy_s(market, 6, "sz");
	};

	tdxlen = snprintf(tdx_path, FILE_NAME_MAX, TDX_PATH_FORMAT, market);
	clwwlen = snprintf(clww_path, FILE_NAME_MAX, CLWW_PATH_FORMAT, market);

	if (access(tdx_path, 0) != 0)
		return -1;

	if (mkdir_reverse(clww_path) != 0)
		return -1;

#if 0
	if (access(clww_path, 0) != 0)
		_mkdir (clww_path);
#endif

	snprintf(tdx_path + tdxlen, FILE_NAME_MAX - tdxlen, "\\%s%s.day", market, code);
	
#if 0
	snprintf(clww_path + clwwlen, FILE_NAME_MAX - clwwlen, "\\%s%s.day", market, code);
#endif
	snprintf(clww_path + clwwlen, FILE_NAME_MAX - clwwlen, "\\%s.day", code);


	tdxfd = _open(tdx_path, _O_RDONLY|_O_BINARY);
	if (tdxfd == -1) {
		//cout << "open tdx file failed" <<endl;
		return(-1);
	}

	clwwfd = _open(clww_path, _O_RDWR | _O_CREAT | _O_BINARY, _S_IREAD | _S_IWRITE);
	if (clwwfd == -1) {
		cout << "open clww file failed" <<endl;
		close(tdxfd);
		return(-1);
	}

	memset(&clwwdata, 0, sizeof(struct clww_day));
	len = read(tdxfd, &tdxdata, sizeof(struct tdx_day));
	while (len == sizeof(struct tdx_day)) {
		//cout << tdxdata.date<< endl;
		clwwdata.day_date = tdxdata.date;
		clwwdata.open_price = tdxdata.open*10;
		clwwdata.high_price = tdxdata.high*10;
		clwwdata.low_price = tdxdata.low*10;
		clwwdata.close_price = tdxdata.close*10;
		clwwdata.day_volume = tdxdata.vol/100;
		clwwdata.day_amount = (DWORD)tdxdata.amount/1000;

		if (write(clwwfd, &clwwdata, sizeof(struct clww_day)) != sizeof(struct clww_day)) {
			close(tdxfd);
			close(clwwfd);
			cout << "write to file failed" <<endl;
			return (-1);
		}
		len = read(tdxfd, &tdxdata, sizeof(struct tdx_day));
	}



	//cout <<  tdx_path << endl;
	//cout << clww_path << endl;

	close(tdxfd);
	close(clwwfd);


	return 0;
}


#define CLWW_ROOT_PATH	"G:\\Dev\\stock\\clww2\\Bin\\data\\"

int prepare_directory()
{
	char clww_path[FILE_NAME_MAX];

	snprintf(clww_path, FILE_NAME_MAX, CLWW_ROOT_PATH, "sh\\day");	
	if (mkdir_reverse(clww_path) != 0)
		return -1;	

	snprintf(clww_path, FILE_NAME_MAX, CLWW_ROOT_PATH, "sz\\day");	
	if (mkdir_reverse(clww_path) != 0)
		return -1;	
}



int main()
{
	int i;
	char buf[20];

	/* 000 */
	for (i = 0; i < 1000; i++) {
		snprintf(buf, 20, "000%03d", i);
		convert(buf);
	}

	convert("001696");
	convert("001896");

	/* 002 */
	for (i = 0; i < 704; i++) {
		snprintf(buf, 20, "002%03d", i);
		convert(buf);
	}	

	/* 300 */
	for (i = 0; i < 357; i++) {
		snprintf(buf, 20, "300%03d", i);
		convert(buf);
	}		

	/* 600 */
	for (i = 0; i < 1000; i++) {
		snprintf(buf, 20, "600%03d", i);
		convert(buf);
	}	


	/* 601 */
	for (i = 0; i < 1000; i++) {
		snprintf(buf, 20, "601%03d", i);
		convert(buf);
	}	

	/* 603 */
	for (i = 0; i < 1000; i++) {
		snprintf(buf, 20, "603%03d", i);
		convert(buf);
	}		

	cout << "helloworld"<<endl;

	


	return 0;
}

